package com.dynamite;

import base.Start;
import base.render.Render;
import base.world.World;

public abstract class Entity extends Render
{
	/**Current x position*/
	protected float posX;
	/**Current y position*/
	protected float posY;
	/**Current z rotation*/
	protected double posRot;

	/**Current x motion**/
	protected float motionX;
	private float pushMotionX;
	/**Current y motion**/
	protected float motionY;
	private float pushMotionY;

	private final float motionInc = 0.1F;
	private final float motionFric = 0.08F;

	protected boolean isDead;

	public Entity(float x, float y, double rot)
	{
		this.posX = x;
		this.posY = y;
		this.posRot = rot;
	}

	public void onUpdate()
	{
		this.updatePushMotion();
		this.updateMotion();

		this.posX += this.motionX;
		this.posY += this.motionY;
	}

	private void updatePushMotion()
	{
		//Push Motion X
		if (this.pushMotionX != 0F)
		{
			if (this.pushMotionX > this.motionInc)
			{
				this.motionX += this.motionInc;
				this.pushMotionX -= this.motionInc;
			}
			else if (this.pushMotionX > 0F)
			{
				this.motionX += this.pushMotionX;
				this.pushMotionX = 0F;
			}
			else if (this.pushMotionX < -this.motionInc)
			{
				this.motionX -= this.motionInc;
				this.pushMotionX += this.motionInc;
			}
			else if (this.pushMotionX < 0F)
			{
				this.motionX += this.pushMotionX;
				this.pushMotionX = 0F;
			}
		}
		
		//Push Motion Y
		if (this.pushMotionY != 0F)
		{
			if (this.pushMotionY > this.motionInc)
			{
				this.motionY += this.motionInc;
				this.pushMotionY -= this.motionInc;
			}
			else if (this.pushMotionY > 0F)
			{
				this.motionY += this.pushMotionY;
				this.pushMotionY = 0F;
			}
			else if (this.pushMotionY < -this.motionInc)
			{
				this.motionY -= this.motionInc;
				this.pushMotionY += this.motionInc;
			}
			else if (this.pushMotionY < 0F)
			{
				this.motionY += this.pushMotionY;
				this.pushMotionY = 0F;
			}
		}
	}

	private void updateMotion()
	{
		//Motion X
		if (this.motionX > this.motionFric)
		{
			this.motionX -= this.motionFric;
		}
		else if (this.motionX < -this.motionFric)
		{
			this.motionX += this.motionFric;
		}
		else
		{
			this.motionX = 0F;
		}

		//Motion Y
		if (this.motionY > this.motionFric)
		{
			this.motionY -= this.motionFric;
		}
		else if (this.motionY < -this.motionFric)
		{
			this.motionY += this.motionFric;
		}
		else
		{
			this.motionY = 0F;
		}
	}

	public void pushInMotion(float par1, float par2)
	{
		this.pushMotionX += par1*motionInc;
		this.pushMotionY += par2*motionInc;
	}

	public void setMotion(float par1, float par2)
	{
		this.pushMotionX = par1;
		this.pushMotionY = par2;
	}

	public float getPosX()
	{
		return this.posX;
	}

	public float getPosY()
	{
		return this.posY;
	}

	public double getPosRot()
	{
		return this.posRot;
	}

	public World getWorldObj()
	{
		return (World) Start.theWorld;
	}

	public boolean isDead()
	{
		return this.isDead;
	}

	public void setDead()
	{
		this.isDead = true;
	}

	public static double getDistToLoc(float x0, float y0, float x1, float y1)
	{
		return Math.sqrt(Math.pow(x0 - x1, 2) + Math.pow(y0 - y1, 2));
	}

	public static double getRotToLoc(float x0, float y0, float x1, float y1)
	{
		return (Math.toDegrees(Math.atan2(y0 - y1, x1 - x0)) + 360) % 360;
	}

	public static double getRotToRot(double angle0, double angle1)
	{
		double var0 = angle1 - angle0;
		return var0 < -180.0D ? (var0 + 360) : var0 < 180.0D ? (var0 - 369) : var0;
	}

	public static double getDistToXRot(double distance, double angle)
	{
		return Math.cos(angle * Math.PI / 180) * distance;
	}

	public static double getDistToYRot(double distance, double angle)
	{
		return -Math.sin(angle * Math.PI / 180) * distance;
	}
}
